﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SD.IBUS;
using SD.Entities;
using SD.BUS;

namespace SD.UI
{
    public partial class SearchAddress : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData(string.Empty, string.Empty, string.Empty);
                if (!String.IsNullOrEmpty(Request.Params["addressId"]))
                {
                    SelectRowOnLoading();
                }
            }
        }

        protected void btnSearchAddress_Click(object sender, EventArgs e)
        {
            IAddressBIZ iAddress = new AddressBIZ();
            LoadData(tbxPostcode.Text.ToString(), tbxStreet.Text.ToString(), tbxTown.Text.ToString());

        }

        protected void btnNone_Click(object sender, EventArgs e)
        {

        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            tbxPostcode.Text = "";
            tbxStreet.Text = "";
            tbxTown.Text = "";
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            if (gvSearchAddress.SelectedValue == null)
            {
                Response.Write("<script>alert('Please Choose Row need Select')</script>");
                return;
            }
            else
            {
                string selectValue = gvSearchAddress.Rows[gvSearchAddress.SelectedIndex].Cells[2].Text;//Request.Form["rdbChoose"];
                string contactValue = gvSearchAddress.SelectedValue.ToString();//Request.Form["contactID"];  
                IAddressBIZ addressBIZ = new AddressBIZ();
                Address address = addressBIZ.GetById(contactValue);
                string line2 = address.Line2;
                string line3 = address.Line3;
                string address1 = gvSearchAddress.Rows[gvSearchAddress.SelectedIndex].Cells[1].Text;
                string town = gvSearchAddress.Rows[gvSearchAddress.SelectedIndex].Cells[3].Text;
                string county = gvSearchAddress.Rows[gvSearchAddress.SelectedIndex].Cells[4].Text;
                string country = gvSearchAddress.Rows[gvSearchAddress.SelectedIndex].Cells[5].Text;
                string str = "var Str = '" + selectValue + "';var ctID='" + contactValue + "';var vAddress='"
                    + address1 + "';var vTown='" + town + "';var vCountry='" + country + "';var vCounty='"
                    + county + "';var vLine2='" + line2 + "';var vLine3='" + line3 +
                    "';window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxAddressLine2').value=vLine2;" +
                    "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxAddressLine3').value=vLine3;" +
                    "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxCounty').value=vCounty;" +
                    "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxNationCountry').value=vCountry;" +
                    "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxAddressLine1').value=vAddress;" +
                    "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxCityTown').value=vTown;" +
                    " window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxPostcode').value=Str;" +
                    "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_txtAddressID').value=ctID;" +
                    "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfLine1').value=vAddress;" +
                    "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfLine2').value=vLine2;" +
                    "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfLine3').value=vLine3;" +
                    "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfPostCode').value=Str;" +
                    "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfTown').value=vTown;" +
                    "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfCounty').value=vCounty;" +
                    "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfCountry').value=vCountry;" +
                    "window.close();";
                ScriptManager.RegisterClientScriptBlock(btnSelect, typeof(Button), "select", str, true);
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {

        }

        private void SelectRowOnLoading()
        {
            for (Int32 i = 0; i < gvSearchAddress.PageCount; i++)
            {
                gvSearchAddress.PageIndex = i;
                DataBind();

                Boolean isExit = false;
                foreach (GridViewRow row in gvSearchAddress.Rows)
                {
                    String keyValue = gvSearchAddress.DataKeys[row.RowIndex].Value.ToString();

                    if (keyValue.Equals(Request.Params["addressId"]))
                    {
                        gvSearchAddress.SelectedIndex = row.RowIndex;
                        RadioButton rdb = (RadioButton)row.FindControl("rdbChoose");
                        rdb.Checked = true;

                        isExit = true;
                        break;
                    }
                }
                if (isExit)
                {
                    break;
                }
            }
        }

        private DataTable LoadData(string postcode, string street, string town)
        {
            IAddressBIZ iAddress = new AddressBIZ();
            DataTable dt = iAddress.ListAddressByCodition(postcode, street, town);

            // If have data in table
            if (dt.Rows.Count > 0)
            {
                gvSearchAddress.DataSource = dt;
                gvSearchAddress.DataBind();
                lblMessage.Text = string.Empty;
            }
            else // if have no data in table
            {
                dt.Rows.Add(dt.NewRow());
                gvSearchAddress.DataSource = dt;
                gvSearchAddress.DataBind();
                gvSearchAddress.Rows[0].Visible = false;
                lblMessage.Text = "No records found";
            }

            return dt;
        }

        protected void rdbChoose_CheckedChanged(object sender, EventArgs e)
        {

            Deselect();
            RadioButton rb = (RadioButton)sender;
            rb.Checked = true;
            GridViewRow row = (GridViewRow)((RadioButton)sender).NamingContainer;

            if (row != null)
            {
                gvSearchAddress.SelectedIndex = row.RowIndex;
            }
        }

        private void Deselect()
        {
            //GridViewRow row = new GridViewRow();
            int i = 0;
            foreach (GridViewRow r in gvSearchAddress.Rows)
            {
                RadioButton rb = (RadioButton)(gvSearchAddress.Rows[i].FindControl("rdbChoose"));
                rb.Checked = false;
                i++;

            }
        }

        protected void gvSearchAddress_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSearchAddress.PageIndex = e.NewPageIndex;
            LoadData(tbxPostcode.Text.ToString(), tbxStreet.Text.ToString(), tbxTown.Text.ToString());
            gvSearchAddress.SelectedIndex = -1;

            foreach (GridViewRow row in gvSearchAddress.Rows)
            {
                String keyValue = gvSearchAddress.DataKeys[row.RowIndex].Value.ToString();

                if (keyValue.Equals(Request.Params["addressId"]))
                {
                    gvSearchAddress.SelectedIndex = row.RowIndex;
                    RadioButton rdb = (RadioButton)row.FindControl("rdbChoose");
                    rdb.Checked = true;
                }
            }
        }
    }
}
